package com.changgou.goods.service.impl;

import com.changgou.goods.dao.CategoryMapper;
import com.changgou.goods.dao.TemplateMapper;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Template;
import com.changgou.goods.service.CategoryService;
import com.changgou.goods.service.TemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: wzw
 * @Date: 2020/12/8 21:56
 * @version: 1.8
 */
@Service
public class TemplateServiceImpl implements TemplateService {

    //注入TemplateMapper
    @Autowired(required = false)
    private TemplateMapper templateMapper;

    //注入CategoryMapper
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageInfo<Template> findPage(Template template, Integer page, Integer size) {
        //设置分页条件
        PageHelper.startPage(page, size);
        //设置查询条件
        Example example = createExample(template);
        //实现功能:执行查询
        List<Template> list = templateMapper.selectByExample(example);
        return new PageInfo<Template>(list);
    }

    private Example createExample(Template template) {
        Example example = new Example(Template.class);
        Example.Criteria criteria = example.createCriteria();
        if (template != null) {
            if (!StringUtils.isEmpty(template.getId())){
                criteria.andEqualTo("id", template.getId());
            }
            if (!StringUtils.isEmpty(template.getName())){
                criteria.andLike("name","%"+template.getName()+"%");
            }
            if (!StringUtils.isEmpty(template.getSpecNum())){
                criteria.andEqualTo("specNum",template.getSpecNum());
            }
            if (!StringUtils.isEmpty(template.getParaNum())){
                criteria.andEqualTo("paraNum",template.getParaNum());
            }
        }
        return example;
    }

    /**
     * Template分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Template> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Template>(templateMapper.selectAll());
    }

    /**
     * Template条件查询
     * @param template
     * @return
     */
    @Override
    public List<Template> findList(Template template){
        //构建查询条件
        Example example = createExample(template);
        //根据构建的条件查询数据
        return templateMapper.selectByExample(example);
    }


    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        templateMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Template
     * @param template
     */
    @Override
    public void update(Template template){
        templateMapper.updateByPrimaryKey(template);
    }

    /**
     * 增加Template
     * @param template
     */
    @Override
    public void add(Template template){
        templateMapper.insert(template);
    }

    /**
     * 根据ID查询Template
     * @param id
     * @return
     */
    @Override
    public Template findById(Integer id){
        return  templateMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Template全部数据
     * @return
     */
    @Override
    public List<Template> findAll() {
        return templateMapper.selectAll();
    }


    /**
     * @author wzw
     * 根据分类ID查询模板信息
     * @Date 21:59 2020/12/10
     * @param categoryId
     * @return com.changgou.goods.pojo.Template
     **/
    @Override
    public Template findByCategoryId(Integer categoryId) {
        //实现功能:先查询商品分类
        Category category = categoryMapper.selectByPrimaryKey(categoryId);

        //实现功能 :根据分类Pojo中的模板id查询模板信息
        Template template = templateMapper.selectByPrimaryKey(category.getTemplateId());

        //返回结果
        return template;
    }
}
